package com.eternal.permission.service.impl;

import com.eternal.permission.entity.PermissionInfo;
import com.eternal.permission.entity.RolePermission;
import com.eternal.permission.dao.RolePermissionMapper;
import com.eternal.permission.entity.enums.PTypeEnum;
import com.eternal.permission.service.PermissionInfoService;
import com.eternal.permission.service.RolePermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.eternal.util.tree.BuildTree;
import com.eternal.util.tree.entity.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author Eternal
 * @since 2018-08-26
 */
@Service
@Primary
@Transactional
@CacheConfig(cacheNames = "rolePermission")
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private PermissionInfoService permissionInfoService;

    @Override
    public List<PermissionInfo> getPermissionByRoleId(String roleId) {
        List<PermissionInfo> permissionInfos = this.baseMapper.selectPermissionByRoleId(roleId);
        return permissionInfos;
    }

    @Override
    public Set<PermissionInfo> getPermissionByRoleIds(List<String> roleIds) {
        Set<PermissionInfo> permissionInfoSet = this.baseMapper.selectPermissionByRoleIds(roleIds);

        return permissionInfoSet;
    }


    @Override
    public List<Tree<PermissionInfo>> getPermission(String roleId) {

        List<PermissionInfo> all = permissionInfoService.getAll();

        List<String> permissionInfos = this.baseMapper.selectPermissionByRoleIdStr(roleId);
        List<Tree<PermissionInfo>> trees = new ArrayList<>();
        for (PermissionInfo permissionInfo : all) {
            Tree<PermissionInfo> tree = new Tree<>();
            tree.setId(permissionInfo.getPId());
            tree.setTitle(permissionInfo.getPName());
            tree.setParentId(permissionInfo.getPParentCode());
            if (permissionInfos.contains(permissionInfo.getPId()) && "1".equals(permissionInfo.getIsLeaf())) {
                tree.setChecked(true);
            } else {
                tree.setChecked(false);
            }
            if (PTypeEnum.MODEL == permissionInfo.getPType()) {
                tree.setExpand(true);
            }
            trees.add(tree);
        }
        List<Tree<PermissionInfo>> build = BuildTree.build(trees);
        return build;
    }

    @Override
    public boolean addPermission(String roleId, String[] permissions) {
        this.deleteById(roleId);
        List<RolePermission> list = new ArrayList<>();
        for (String permission : permissions) {
            list.add(new RolePermission(roleId, permission));
        }
        return this.baseMapper.insertBatch(list);
    }
}
